package com.example.himalaya.view.fragments;

import android.content.Intent;
import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.himalaya.view.activity.DetailActivity;
import com.example.himalaya.R;
import com.example.himalaya.view.adapters.RecommendListAdapter;
import com.example.himalaya.base.BaseFragment;
import com.example.himalaya.view.callback.IRecommendViewCallback;
import com.example.himalaya.presenters.AlbumDetailPresenter;
import com.example.himalaya.presenters.RecommendPresenter;
import com.example.himalaya.view_component.UILoader;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.ximalaya.ting.android.opensdk.model.album.Album;

import net.lucode.hackware.magicindicator.buildins.UIUtil;

import java.util.List;
import java.util.Objects;

/**
 * 获取推荐内容部分
 */
public class RecommendFragment extends BaseFragment implements IRecommendViewCallback, RecommendListAdapter.onRecommendItemClickListener {

    // Variable
    private static final String TAG = "RecommendFragment";
    private View mSuccessView;
    // 内容部分
    private RecyclerView mRecommendRv;
    // 内容部分的数据适配器
    private RecommendListAdapter mRecommendListAdapter;

    private RecommendPresenter mRecommendPresenter;

    private UILoader mUiLoader;

    @Override
    protected View onSubViewLoaded(final LayoutInflater layoutInflater, ViewGroup container) {

        mUiLoader = new UILoader(Objects.requireNonNull(getContext())) {
            @Override
            protected View getSuccessView(ViewGroup container) {
                return createSuccessView(layoutInflater, container);
            }
        };

        initPresenter();

        if (mUiLoader.getParent() instanceof ViewGroup) {
            ((ViewGroup) mUiLoader.getParent()).removeView(mUiLoader);
        }

        initListener();

        initData();

        // 返回view, 给界面显示
        return mUiLoader;
    }

    private void initListener() {
        mUiLoader.setOnRetryClickListener(new UILoader.OnRetryClickListener() {
            @Override
            public void onRetryClick() {
                // 表示网络不佳的时候，用户点击了重试

                if (mRecommendPresenter != null) {
                    mRecommendPresenter.getRecommendList();
                }
            }
        });
    }

    private void initData() {
        // 获取推荐列表
        mRecommendPresenter.getRecommendList();
    }

    private void initPresenter() {
        // 获取到逻辑层的对象
        mRecommendPresenter = RecommendPresenter.getInstance();

        // 先要设置通知接口的注册
        mRecommendPresenter.registerViewCallback(this);
    }


    /**
     * 根据返回的数据生成界面的方法
     *
     * @param layoutInflater 不知道
     * @param container      父组件
     * @return 根组件
     */
    private View createSuccessView(LayoutInflater layoutInflater, ViewGroup container) {
        // 1. View 加载完成
        // 获取对象
        mSuccessView = layoutInflater.inflate(R.layout.fragment_recommend, container, false);

        TwinklingRefreshLayout twinklingRefreshLayout = mSuccessView.findViewById(R.id.over_scroll_view);


        twinklingRefreshLayout.setPureScrollModeOn(true);

        mRecommendRv = mSuccessView.findViewById(R.id.recommend_list);

        // 2.设置布局管理器
        LinearLayoutManager linerLayoutManager = new LinearLayoutManager(getContext());
        // 设置布局方向(垂直）
        linerLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecommendRv.setLayoutManager(linerLayoutManager);
        mRecommendRv.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                outRect.top = UIUtil.dip2px(view.getContext(), 5);
                outRect.bottom = UIUtil.dip2px(view.getContext(), 5);
                outRect.left = UIUtil.dip2px(view.getContext(), 5);
                outRect.right = UIUtil.dip2px(view.getContext(), 5);
            }
        });

        // 3. 设置适配器， 控制内容的显示
        mRecommendListAdapter = new RecommendListAdapter();
        mRecommendRv.setAdapter(mRecommendListAdapter);
        mRecommendListAdapter.setOnRecommendItemClickListener(this);

        return mSuccessView;
    }


    @Override
    public void onRecommendListLoaded(List<Album> result) {
        // 数据回来了，就是跟新UI了
        mRecommendListAdapter.setData(result);


        // 切换到成功收到手机的界面
        mUiLoader.updateStatus(UILoader.UIStatus.SUCCESS);
    }


    @Override
    public void onNetworkError() {
        mUiLoader.updateStatus(UILoader.UIStatus.ERROR);

    }

    @Override
    public void onEmpty() {
        mUiLoader.updateStatus(UILoader.UIStatus.EMPTY);

    }

    @Override
    public void onLoading() {
        mUiLoader.updateStatus(UILoader.UIStatus.LOADING);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        // 取消接口的注册
        if (mRecommendPresenter != null) {
            mRecommendPresenter.unRegisterViewCallback(this);
        }
    }


    @Override
    public void onItemClick(int position, Album album) {

        AlbumDetailPresenter.getInstance().setTargetAlbum(album);

        // item被点击了, 跳到当前item的详情界面
        // 根据位置拿到数据

        Intent intent = new Intent(getContext(), DetailActivity.class);

        startActivity(intent);
    }
}
